﻿using Starzero.Framework.Identity.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApp.Services;
using WebApiClient;
using Microsoft.Extensions.Logging;
using OSharp.Identity;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //ֻ��Ҫ��ʼ��һ��
            HttpApiFactory.Add<IIdentityService>().ConfigureHttpApiConfig(c =>
            {
                c.HttpHost = new Uri("http://localhost:5000/");
                c.LoggerFactory = new LoggerFactory().AddConsole();
                c.FormatOptions.DateTimeFormat = DateTimeFormats.ISO8601_WithMillisecond;
                c.GlobalFilters.Add(new RequestGlobalFilter());
            });

            Login();

            Console.ReadKey();
        }

        public static async void Login()
        {
            //login model
            var login = new LoginDto();
            login.Account = "admin";
            login.Password = "123qwe";

            //��ʼ��¼
            var client = HttpApiFactory.Create<IIdentityService>();
            //��¼��ʽ1��ͬ����ʽ
            var user1 = client.Jwtoken(login)
                .Retry(3, i => TimeSpan.FromSeconds(i))
                .WhenCatch<HttpStatusFailureException>()
                .ToObservable()
                .Subscribe(result =>
                {
                    ServiceConsts.Token = result.Data as string;
                    Console.WriteLine(ServiceConsts.Token);

                    client.Profile()
                        .Retry(3, i => TimeSpan.FromSeconds(i))
                        .WhenCatch<HttpStatusFailureException>()
                        .ToObservable()
                        .Subscribe(r =>
                        {
                            Console.WriteLine(r);
                        }, ex =>
                        {
                            Console.WriteLine(ex);
                        });
                }, ex =>
                {
                    Console.WriteLine(ex);
                });

            //��¼��ʽ1���첽��ʽ���Ƽ�
            var user2 = await client.Jwtoken(login)
                .Retry(3,i=> TimeSpan.FromSeconds(i))
                .WhenCatch<HttpStatusFailureException>();
            Console.WriteLine(user2.Data);
        }
    }
}

